﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Enities;

namespace QLThuVien_Nhom21
{
    public partial class QuanLyDocGia : Form
    {
        public QuanLyDocGia()
        {
            InitializeComponent();
        }
        LuuTruDocGia docgia = new LuuTruDocGia();
        void Load()
        {
            dtgwQLDG.DataSource=docgia.SelectAll();

        }
        private void bttdanhsachQL_Click(object sender, EventArgs e)
        {
            Load();

        }

        private void btnthem_Click(object sender, EventArgs e)
        {

           try
            {
                DocGia dg = new DocGia();
                dg.MaDocGia = int.Parse(txtMaDGQL.Text);
                dg.HoTenDocGia = txttendocgia.Text;
                dg.NgaySinh = dtpngaysinh.Value;
                dg.DiaChi = txtdiachi.Text;
                dg.Email = txtemail.Text;
                dg.LoaiDocGia= cboloaidocgia.Text;
                docgia.Them(dg);
                Load();
                XoaTrang();
            }
            
            catch
            {
                 if (txtMaDGQL.Text.Length != 7)
            {
                MessageBox.Show("phai nhap 7 ky tu", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            }

        }
        
        

        

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (txtMaDGQL.Text == "")
            {
                MessageBox.Show("nhap vao ma", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                docgia.Xoa(int.Parse(txtMaDGQL.Text));
            }
            Load();
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txtMaDGQL.Text == "")
            {
                MessageBox.Show("nhap vao ma", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                DocGia dg = new DocGia();
                dg.MaDocGia = int.Parse(txtMaDGQL.Text);
                dg.HoTenDocGia = txttendocgia.Text;
                dg.NgaySinh = dtpngaysinh.Value;
                dg.DiaChi = txtdiachi.Text;
                dg.Email = txtemail.Text;
                dg.LoaiDocGia = cboloaidocgia.Text;
                docgia.Sua(dg);
            }
            Load();
        }
        public void XoaTrang()
        {
            txtMaDGQL.Text = "";
            txttendocgia.Text = "";
            txtdiachi.Text = "";
            txtemail.Text = "";

        }

        private void btntimkiem_Click(object sender, EventArgs e)
        {
            if (txtMaDGTK.Text == "" || txttendgiaTK.Text=="")
            {
                MessageBox.Show("nhap vao tu khoa tim kiem", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {

                    dtgwTKDG.DataSource = docgia.timkiemMa(int.Parse(txtMaDGTK.Text), txttendgiaTK.Text);
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btndanhsachTK_Click(object sender, EventArgs e)
        {
            dtgwTKDG.DataSource = docgia.SelectAll();
        }

        private void btnthoatTK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
